#include<unistd.h>
#include<time.h>
#include"BlockQueue.hpp"
#include"Task.hpp"
void *Consumer(void *args)
{
    BlockQueue<Task>*bp=static_cast<BlockQueue<Task>*>(args);
    while(true){
        Task number=bp->pop();
        int ans=number.run();
        //cout << "消费了一个数据:" <<number<<"  "<< endl;
        cout << "处理了一个数据结果是:" <<ans<<"  "<< endl;
    }

}
void *Productor(void *args)
{
    BlockQueue<Task>*bp=static_cast<BlockQueue<Task>*>(args);
    while(true)
    {
        int x=rand()%10+1;
        usleep(5);
        int y=rand()%10+1;
        Task n(x,y);
        bp->push(n);
        cout << "生产了一个数据: "<<n.get()<< endl;
        sleep(1);
    }
}
int main()
{
    srand(time(nullptr));
    BlockQueue<int>*bp=new BlockQueue<int>(20);
    vector<pthread_t>tids;
    pthread_t tid1,tid2;    
    pthread_create(&tid1,nullptr,Consumer,bp);  
    pthread_create(&tid2,nullptr,Productor,bp);
    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);
    delete bp;
    return 0;
}